package cn.baiweigang.uu.utils;

import cn.baiweigang.uu.model.DataMap;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @author 玄罗
 * @date 2021/02/07
 */
public class DButil {
    private static  String USER_NAME = "";
    private static  String PWD = "";
    private static  String PORT = "";
    private static  String DB_NAME = "";

    private static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
    private static Connection conn = null;
    private static Statement statement = null;

    private static void init() {
        try {
            //读取DB配置
            USER_NAME = PropertiesUtil.getProperty("DB_USER_NAME");
            PWD = PropertiesUtil.getProperty("DB_PWD");
            PORT = PropertiesUtil.getProperty("DB_PORT");
            DB_NAME = PropertiesUtil.getProperty("DB_NAME");
            if (conn==null || conn.isClosed()){
                // 注册 JDBC 驱动
                Class.forName(JDBC_DRIVER);
                // 打开链接
                LogUtil.logInfo("连接数据库...");
                conn = DriverManager.getConnection("jdbc:mysql://localhost:" + PORT + "/" + DB_NAME + "?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai",
                        USER_NAME, PWD);
                statement = conn.createStatement();
            }
        } catch (Throwable e) {
            LogUtil.logInfo(e.getMessage());
        }
    }

    private static void closeConn() {
//        try {
//            // 关闭DB链接
//            LogUtil.logInfo("执行SQL完毕 断开数据库");
//            statement.close();
//            conn.close();
//        } catch (Throwable e) {
//            LogUtil.logInfo(e.getMessage());
//        }
    }

    public static DataMap query(String sql) {
        List<DataMap> dataMapList=queryMultil(sql);
        if (dataMapList!=null && dataMapList.size()>0){
            return dataMapList.get(0);
        }else {
            return null;
        }
    }
    public static List<DataMap> queryMultil(String sql) {
        List<DataMap> dataList = new ArrayList<DataMap>();
        DataMap map = null;
        try {
            init();
            ResultSet resultSet = statement.executeQuery(sql);
            ResultSetMetaData rsmd = resultSet.getMetaData();
            while (resultSet.next()) {
                map = new DataMap();
                for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                    map.put(rsmd.getColumnName(i), resultSet.getObject(i));
                }
                dataList.add(map);
            }
        } catch (SQLException e) {
            LogUtil.logInfo("执行SQL异常 " + e.getMessage());
        } finally {
            closeConn();
        }
        return dataList;
    }

    public static int update(String sql) {
        int resultSet = 0;
        try {
            init();
            resultSet = statement.executeUpdate(sql);
        } catch (Throwable e) {
            LogUtil.logInfo("更新数据库出错 " + e.getMessage());
            return -1;
        } finally {
            closeConn();
        }
        return resultSet;
    }
}
